Numeric representations of data types.html (7080B)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3 <html> 4 <head> 5 <link rel="stylesheet" href="sitewide.css" /> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 7 <meta name="exporter-version" content="Evernote Mac 6.13.1 (455785)"/> 8 <meta name="altitude" content="1.558525204658508"/> 9 <meta name="author" content="Alex Balgavy"/> 10 <meta name="created" content="2017-11-14 3:02:33 PM +0000"/> 11 <meta name="latitude" content="52.3330093917089"/> 12 <meta name="longitude" content="4.865526562425694"/> 13 <meta name="source" content="desktop.mac"/> 14 <meta name="updated" content="2017-12-05 10:22:21 PM +0000"/> 15 <title>Numeric representations of data types</title> 16 </head> 17 <body> 18 <div>Everything’s stored in binary. Obviously. This is computers.</div> 19 <div><br/></div> 20 <div><span style="font-weight: bold;">Signed Integers</span></div> 21 <div>Representing both positive and negative numbers.</div> 22 <div>Leftmost bit (MSB) tells state of sign flag — 0 for positive and 1 for negative</div> 23 <div><br/></div> 24 <div>Systems:</div> 25 <div> 26 <ul> 27 <li>Sign-and-magnitude</li> 28 <ul> 29 <li>negative values are represented by changing MSB</li> 30 <li>two representations for 0 — ±0</li> 31 </ul> 32 <li>1’s-complement</li> 33 <ul> 34 <li>negative values are bitwise complement of positive</li> 35 <li>for n-bit, equivalent to subtracting number from 2<span style="vertical-align: super;">n</span>-1</li> 36 <li>two representations for 0 — ±0</li> 37 </ul> 38 <li>2’s-complement</li> 39 <ul> 40 <li>1’s-complement; then add 1</li> 41 <li>in other words: for n-bit, subtract number from 2<span style="font-size: 11.666666030883789px;"><span style="font-size: 11.666666030883789px; vertical-align: super;">n</span></span></li> 42 <li><span style="font-size: 14px;">one representation for 0</span></li> 43 <li><span style="font-size: 14px;">can represent -8 in 4 bits</span></li> 44 </ul> 45 </ul> 46 </div> 47 <div><br/></div> 48 <div><a href="Addition%20%26%20subtraction%20with%20signed%20integers.html">Arithmetic operations with signed integers.</a></div> 49 <div><a href="Addition_subtraction%20logic%20unit.html">How to design an actual circuit for this shit.</a></div> 50 <div><a href="Multiplication%20of%20signed%20integers.html">Multiplication of signed integers</a>.</div> 51 <div>Division is a pain in the ass, exactly the same as decimal long division. Just with 1s and 0s.</div> 52 <div><br/></div> 53 <div><span style="font-weight: bold;">Floats</span></div> 54 <div>float in binary: sign for number, significant bits, signed scale factor exponent for implied base 2</div> 55 <div>IEEE standard (32 bit floats) — sign bit, 8-bit signed exponent in excess-127, 23-bit mantissa (fractional)</div> 56 <div><br/></div> 57 <div><img src="Numeric%20representations%20of%20data%20types.resources/screenshot.png" height="151" width="494"/><br/></div> 58 <div><br/></div> 59 <div>The value stored in exponent is unsigned int E’ = E + 127 (excess-127).</div> 60 <div>E being unsigned int representation, E’ being excess 127.</div> 61 <div><br/></div> 62 <div>Why excess-127? In 32 bits, you have 8 bits for the exponent. With 8 bits, you can represent values 0 to 255. But we want really small numbers, so a negative exponent. So the dudes at IEEE decided to go for -127 to +128. -127 (0) represents 0, 128 (255) represents infinity. So real range is -126 to +127. But the value in the exponent is an unsigned int, from 0 to 255, so the whole thing has to be shifted. Just define 0 to be -127 and you’re done. In other words, if you put a 0 in the exponent, you’re actually representing -127.</div> 63 <div><br/></div> 64 <div>Confusing as shit. Basically if you want to write some value, you have to put that value + 127 in the exponent, in binary.</div> 65 <div><br/></div> 66 <div>To convert to excess-127:</div> 67 <div> 68 <ul> 69 <li>convert in front of decimal point to binary (divide by 2 until no remainder, bits are in bottom-to-top order)</li> 70 <li>convert after decimal point to binary (multiply by 2, left of decimal is next fractional 0 or 1, repeat with right of decimal)</li> 71 <li>normalise it so that it’s of the format “1.<span style="font-style: italic;">M”, </span>note the exponent E</li> 72 <li>add 127 to E to form E’</li> 73 <li><span style="font-style: italic;">M</span> is mantissa, E’ is exponent</li> 74 </ul> 75 <div><br/></div> 76 <div>The number is normalised if it’s in the form “1.something × 2<span style="vertical-align: super; font-size: smaller; font-size: smaller;">n</span>”.</div> 77 </div> 78 <div><br/></div> 79 <div>Special values of mantissa:</div> 80 <div> 81 <ul> 82 <li>exponent all 0, mantissa all 0 — 0</li> 83 <li>exponent all 1, mantissa all 0 — ±Infinity</li> 84 <li>exponent all 0, mantissa not 0 — denormalised numbers (implied 0 instead of 1)</li> 85 <li>exponent all 1, mantissa not 0 — Not a Number</li> 86 </ul> 87 <div><br/></div> 88 </div> 89 <div>All operations use guard bits to keep accuracy. However, to store, you need to remove guard bits (truncate).</div> 90 <div>Methods:</div> 91 <div> 92 <ul> 93 <li>chopping — literally just slice off any extra bits</li> 94 <li>von Neumann rounding — if the ones you remove are all 0, you chop them. but if any of them are 1, the LSB of the retained bits is set to 1.</li> 95 <li>rounding — 1 added to value at LSB of retained if MSB of removed bits is 1. this rounds to an even number.</li> 96 </ul> 97 </div> 98 <div><br/></div> 99 <div><a href="Adding_subtracting%20floating%20point%20values.html">Adding/subtracting floating point values</a>.</div> 100 <div><a href="Multiplying_dividing%20floats.html">Multiplying/dividing floating point values.</a></div> 101 <div><br/></div> 102 <div><span style="font-weight: bold;">Booleans</span></div> 103 <div>false — 00000000</div> 104 <div>true — literally anything else</div> 105 <div><br/></div> 106 <div><span style="font-weight: bold;">Characters</span></div> 107 <div>Common encoding is ASCII. Characters are represented by 7-bit codes. Alphabetic and numeric characters are in increasing sequential order.</div> 108 <div>Unicode has a large set of international alphabets, with variable width encoding (1-4 bytes, ASCII to Latin/Greek/Cyrillic/Coptic to Chinese/Hindi/tagalog to whatever else)</div> 109 <div><br/></div> 110 </body> 111 </html>